package com.cahnggou.util;

import com.cahnggou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.ServerInfo;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @Author: HuxiWang
 * @Date: Create in 14:53 2021/1/3
 * @Description: 实现文件的工具类
 * 文件的增删改查
 * storage信息获取
 * tracker信息获取
 * <p>
 * fastdfs文件上传:
 * 1.需要先找到tracker   找ip和端口
 */
public class FastDFSUtil {

    /*
        加载tracker连接信息
     */
    static {
        try {
            // 加载路径
            String fileName = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
        文件上传
        @param fastDFSFile
        上传文件信息封装
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
        // 创建一个tracker访问的客户端对象 trackerClient
        TrackerClient trackerClient = new TrackerClient();
        // 通过trackerClient访问trackerServer,获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        // 通过trackerServer的连接信息,获取storage的连接信息, 创建storageClient对象,创建连接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);
        // 通过storageClient,访问storage,实现文件上传, 获取文件存储信息
        /*
            参数:
                1.文件的字节数组
                2.文件扩展名
                3.附加参数
         */
        byte[] content = fastDFSFile.getContent();
        String ext = fastDFSFile.getExt();
        // 附加参数
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", fastDFSFile.getAuthor());
        /*
            返回两个参数
                1.uploads[0]: 文件上传存储的storage的组名字  group1  组名
                2.uploads[1]: 文件存到storage的文件名字  M00/02/44/itheima.jpg
         */
        String[] uploads = storageClient.upload_file(content, ext, meta_list);
        return uploads;
    }

    /*
        获取文件信息
        @param groupName 文件组名
        @param remoteName 文件的存储路径名字
     */
    public static FileInfo getFileInfo(String groupName, String remoteName) throws Exception {
        // 创建trackerClient对象,然后访问trackerServer
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        // 通过trackerClient获取storage连接对象
        StorageClient storageClient = new StorageClient(trackerServer, null);
        // 通过trackerServer获取storage信息,然后存储
        // 获取文件信息
        FileInfo file_info = storageClient.get_file_info(groupName, remoteName);
        return file_info;
    }

    /*
        文件下载
            1.返回字节数组
     */
    public static InputStream downFile(String groupName, String remoteName) throws Exception {
        TrackerServer trackerServer = new TrackerClient().getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        // 下载
        byte[] bytes = storageClient.download_file(groupName, remoteName);
        return new ByteArrayInputStream(bytes);
    }

    /*
        文件删除
            1. 返回删除
            这里要设置删除nginx的缓存  add_header Cache-Control no-store;
     */
    public static int delFile(String groupName, String remoteName) throws Exception {
        TrackerServer trackerServer = new TrackerClient().getConnection();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        // 删除
        int count = storageClient.delete_file(groupName, remoteName);
        return count;
    }

    /*
        获取storage的信息(组名等)
     */
    public static StorageServer getStorageInfo() throws Exception {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        // 获取storage信息
        StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
        return storeStorage;
    }

    /*
        获取storage组的ip和端口
     */
    public static ServerInfo[] getStorageIpAndPort(String groupName, String remoteName) throws Exception {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getFetchStorages(trackerServer, groupName, remoteName);
    }

    //获取tracker 的ip和端口的信息
    //http://192.168.211.132:8080
    public static String getTrackerUrl() {
        try {
            TrackerServer trackerServer = getTrackerServer();
            // tracker 的ip的信息
            String hostString = trackerServer.getInetSocketAddress().getHostString();
            // http://192.168.211.132:8080/group1/M00/00/00/wKjThF1aW9CAOUJGAAClQrJOYvs424.jpg img
            // 获取端口
            int g_tracker_http_port = ClientGlobal.getG_tracker_http_port();
            return "http://" + hostString + ":" + g_tracker_http_port;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
        代码优化
     */
    public static TrackerServer getTrackerServer() throws Exception {
        return new TrackerClient().getConnection();
    }

    public static TrackerClient getTrackClient() throws Exception {
        return new TrackerClient();
    }
}















